<?php

namespace App\Http\Controllers\Video;

use Illuminate\Http\Request;
use App\Models\Video;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class UploadVideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {	
		$video = new Video();
		$arr = $request->input("title");
		//dd($arr);
		if(Input::has('title')){
			$data = DB::table("video")->where("title","like","%$arr%")->paginate(10);
			
		}else{
			
			$data = $video->getList();
		}
        
        //dd($data);
        return view("video.index",['list'=>$data,'title'=>$arr]);
    }

/**
 * 添加视频
 * @return [type] [description]
 */
    public function create()
    {
        
        return view("video.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->input();
        $newData = array_slice($data, 2, 3);
        //dd($newData);

        $video = new Video();
        $re =  $video ->dataStore($newData);
        if($re){
            return redirect("video/index");
        }

    }

   /**
    * 编辑页
    */
   public function edit(Request $request){
        $id = $request->input("id");
        //dd($id);
        $data = DB::table("video")->where("id",$id)->get();
		
        return view("video.edit",["data"=>$data[0]]);
   }
   /**
    *
    * 更新
    */
   public function newStore(Request $request){
        $data = $request->input();
        $id = $data['id'];
        $newData = array_slice($data,2,3);
        $video = new Video();
        $re = $video->updateData($id,$newData);
        if($re){
             return redirect("video/index");
        }
        
   }
   /**
    * 删除+删除视频
    */
   public function deleteData(Request $request){
        $id = $request->input("id");
        $videoUrl = DB::table("video")->where("id",$id)->select("url")->get();
        $href = $videoUrl[0]->url;
        $arr = explode("/",$href);
        $newArr = array_slice($arr,3);
        $str = "";
        foreach($newArr as $v){
            $str.= "/".$v;
        }
        $newStr = substr($str,1);
        $video = new Video();
        $re = $video->deleteData($id,$str);
       if($re){
            $re = $video->deleteVideo($newStr); 
            return redirect("video/index");
       }
   }
	/**
	*删除视频
	*/
	public function delVideo(Request $request){
		$url = $request->input("url");
		$id = $request->input("id");
		$arr = explode("/",$url);
        $newArr = array_slice($arr,3);
        $str = "";
        foreach($newArr as $v){
            $str.= "/".$v;
        }
        $newStr = substr($str,1);
		$video = new Video();
		$re = $video->deleteVideo($newStr); 
		if($re){
			$data['old_url'] = "";
			$video->updateData($id,$data);
			echo "<script> alert('删除成功');window.location.href=document.referrer;</script>";
		}else{
			return redirect("video/index");
		}
		
		
	}
   
   

   
   
   
   
   
   
   
   
   
   
}
